//
//  SERIRankViewController.m
//  WatchBall
//
//  Created by lanou3g on 15/6/4.
//  Copyright (c) 2015年 lanou3g. All rights reserved.
//

#import "SERIRankViewController.h"
#import "UIImageView+WebCache.h"
@interface SERIRankViewController ()

@end

@implementation SERIRankViewController

- (void)viewDidLoad {
    
    [super viewDidLoad];
    
    self.navigationController.navigationBar.translucent = NO;
    CGRect fram=self.view.frame;
    fram.origin.x=CGRectGetWidth(self.view.frame)*3;
    fram.size.height=CGRectGetHeight(self.view.frame)-44;
    self.view.frame=fram;
    self.allArray=[NSMutableArray array];
    self.titleArray=[NSMutableArray array];
    //
    self.view.backgroundColor=[UIColor whiteColor];
    NSArray *array=[NSArray arrayWithObjects:@"积分榜",@"射手榜",@"助攻榜", nil];
    self.segmentVC=[[UISegmentedControl alloc] initWithItems:array];
    CGRect frame = [UIScreen mainScreen].bounds;
    
    _segmentVC.frame=CGRectMake(0,0,frame.size.width, 30);
    self.title=@"中超";
    [_segmentVC addTarget:self action:@selector(exchangeView:) forControlEvents:(UIControlEventValueChanged)];
    _segmentVC.backgroundColor=[UIColor whiteColor];
    _segmentVC.selectedSegmentIndex=0;
    [self.view addSubview:_segmentVC];
    //去掉颜色
    self.segmentVC.tintColor =[UIColor clearColor];
    //设置选中字体的颜色
    NSDictionary* selectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:14],NSForegroundColorAttributeName: [UIColor whiteColor]};
    [self.segmentVC setTitleTextAttributes:selectedTextAttributes forState:UIControlStateSelected];
    //设置未选中的字体颜色
    NSDictionary* unselectedTextAttributes = @{NSFontAttributeName:[UIFont boldSystemFontOfSize:12],NSForegroundColorAttributeName: [UIColor colorWithRed:72/255.0 green:105/255.0 blue:141/255.0 alpha:1.0]};
    [self.segmentVC setTitleTextAttributes:unselectedTextAttributes forState:UIControlStateNormal];
    self.segmentVC.backgroundColor=[UIColor colorWithRed:26/255.0 green:52/255.0 blue:78/255.0 alpha:1.000];
    
    
    self.scoreView=[[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_segmentVC.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-94)];
    //_scoreView.backgroundColor=[UIColor colorWithRed:0.479 green:1.000 blue:0.956 alpha:1.000];
    
    //添加Button
//    UIButton *button=[[UIButton alloc] initWithFrame:CGRectMake(CGRectGetWidth(self.view.frame)/6*5,CGRectGetHeight(_scoreView.frame)/3, 50, 50)];
//    [_scoreView addSubview:button];
//    button.backgroundColor=[UIColor clearColor];
//    [button setTitle:@"球队" forState:(UIControlStateNormal)];
//    button.titleLabel.font=[UIFont systemFontOfSize:10];
//    [button addTarget:self action:@selector(loveTeam:) forControlEvents:(UIControlEventTouchUpInside)];
//    button.layer.cornerRadius=button.frame.size.width/2;
//    button.layer.masksToBounds=YES;
//    [button setImage:[UIImage imageNamed:@"ball3"] forState:(UIControlStateNormal)];
    
    
    
    
    self.shooterView=[[UITableView alloc] initWithFrame:CGRectMake(0,CGRectGetHeight(_segmentVC.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-94)];
    _scoreView.delegate=self;
    _scoreView.dataSource=self;
    _shooterView.delegate=self;
    _shooterView.dataSource=self;
    
    // [self.view addSubview:_scoreView];
    // [self.view addSubview:_shooterView];
    self.assistView=[[UITableView alloc] initWithFrame:CGRectMake(0, CGRectGetHeight(_segmentVC.frame), CGRectGetWidth(self.view.frame), CGRectGetHeight(self.view.frame)-94)];
    _assistView.delegate=self;
    _assistView.dataSource=self;
  //  _assistView.backgroundColor=[UIColor colorWithRed:1.000 green:0.349 blue:0.092 alpha:1.000];
    [self getinformage];
}


//- (void)viewWillAppear:(BOOL)animated{
//
//    NSLog(@"%@,rankView",self.view);
//    CGRect frame = self.view.frame;
//    frame.origin.y = 0;
//    self.view.frame =frame;
//}
- (void)exchangeView:(UISegmentedControl *)segment
{
    switch (segment.selectedSegmentIndex) {
        case 0:
        
            if (_scoreView.superview==nil) {
                [_shooterView removeFromSuperview];
                [_assistView removeFromSuperview];
                [self getinformage];
                [_scoreView reloadData];
                [self.view addSubview:_scoreView];
            }
            break;
        case 1:
        
            if (_shooterView.superview==nil) {
                [_scoreView removeFromSuperview];
                [self getinformage];
                [_assistView removeFromSuperview];
                [_shooterView reloadData];
                [self.view addSubview:_shooterView];
               // _shooterView.backgroundColor=[UIColor colorWithRed:0.437 green:0.461 blue:1.000 alpha:1.000];
                // [_shooterView reloadData];
            }
            break;
        case 2:
        
            if (_assistView.superview==nil) {
                [_scoreView removeFromSuperview];
                [_shooterView removeFromSuperview];
                [self getinformage];
                [_assistView reloadData];
               // _assistView.backgroundColor=[UIColor colorWithRed:1.000 green:0.879 blue:0.124 alpha:1.000];
                [self.view addSubview:_assistView];
                
                //[_assistView reloadData];
            }
            break;
        default:
            break;
    }
    
    
}

//- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
//{
//
//    return 100;
//}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
 #pragma mark - Navigation
 
 // In a storyboard-based application, you will often want to do a little preparation before navigation
 - (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
 // Get the new view controller using [segue destinationViewController].
 // Pass the selected object to the new view controller.
 }
 */

- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView
{
    
    return 1;
}
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
    return @" ";
}
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return _array.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_segmentVC.selectedSegmentIndex==0) {
        ScoreViewCell *cell=[tableView dequeueReusableCellWithIdentifier:@"myCell"];
        //if (!cell) {
        cell=[[ScoreViewCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"myCell"];
        //}
        NSMutableArray *array1=_array[indexPath.row];
        cell.lable1.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:0]];
        cell.lable1.textAlignment=1;
        
        if ([[array1 objectAtIndex:0] intValue]<4) {
            
            cell.lable1.backgroundColor=[UIColor redColor];
            cell.lable1.layer.cornerRadius = cell.lable1.bounds.size.width/2;
            cell.lable1.layer.masksToBounds = YES;
            
            // cell.lable1.layer.cornerRadius=50;
            //  cell.lable1.layer.shadowColor=[UIColor redColor].CGColor;
        }
        
        cell.lable7.text=[array1 objectAtIndex:7];
        cell.lable2.text=[array1 objectAtIndex:1];
        NSString *str=[array1 objectAtIndex:2];
        [cell.imageViews sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeHold"]];
        
        cell.lable3.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:3]];
        cell.lable4.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:4]];
        cell.lable5.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:5]];
        cell.lable6.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:6]];
        cell.lable8.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:8]];
        return cell;
    }
    else
        if (_segmentVC.selectedSegmentIndex==1) {
            CHLGshooterCell *cell=[tableView dequeueReusableCellWithIdentifier:@"shootCell"];
            if (!cell) {
                cell=[[CHLGshooterCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"shootCell"];
            }
            NSMutableArray *array1=_array[indexPath.row];
            
            cell.numberLable.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:0]];
            
            //cell.numberLable.backgroundColor=[UIColor colorWithRed:0.094 green:0.996 blue:0.178 alpha:1.000];
            cell.numberLable.layer.cornerRadius = cell.numberLable.bounds.size.width/2;
            cell.numberLable.layer.masksToBounds = YES;
            cell.nameLable.text=array1[1];
            NSString *str=[array1 objectAtIndex:2];
            [cell.headiconImageview sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeHold"]];
            cell.teamLable.text=array1[3];
            cell.ballcountLable.text=array1[4];
            
            
            return cell;
        }
        else{
            
            CHLGshooterCell *cell=[tableView dequeueReusableCellWithIdentifier:@"cell"];
        if (!cell) {
                cell=[[CHLGshooterCell alloc] initWithStyle:(UITableViewCellStyleDefault) reuseIdentifier:@"cell"];
            }
            NSMutableArray *array1=_array[indexPath.row];
            
            cell.numberLable.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:0]];
           // cell.numberLable.backgroundColor=[UIColor colorWithRed:0.224 green:0.380 blue:0.599 alpha:1.000];
            cell.numberLable.layer.cornerRadius = cell.numberLable.bounds.size.width/2;
            cell.numberLable.layer.masksToBounds = YES;
            cell.nameLable.text=array1[1];
            NSString *str=[array1 objectAtIndex:2];
            [cell.headiconImageview sd_setImageWithURL:[NSURL URLWithString:str] placeholderImage:[UIImage imageNamed:@"placeHold"]];
            
            cell.teamLable.text=array1[3];
            cell.ballcountLable.text=[NSString stringWithFormat:@"%@",[array1 objectAtIndex:4]];;
            return cell;
        }
}

- (void)getinformage
{
    NSString *string=[NSString stringWithFormat:SERI];
    NSURL *url=[NSURL URLWithString:string];
    NSData *data=[NSData dataWithContentsOfURL:url];
    if (data==nil) {
        return ;
    }
    NSDictionary *dic=[NSJSONSerialization JSONObjectWithData:data options:(NSJSONReadingMutableContainers) error:nil];
    _allArray=[dic objectForKey:@"result"];
    //  NSLog(@"%@",_allArray);
    if (_segmentVC.selectedSegmentIndex==0) {
        
        //  _allArray=_scoreView.array;
        // _allArray=_scoreView.array;
        //   NSLog(@"--+++%@",_allArray);
        [self.view addSubview:_scoreView];
        NSDictionary *dic=_allArray[0];
        NSArray *arr=[dic objectForKey:@"ranks_data"];
        // NSLog(@"+-+-%@",_array);
        _titleArray=[arr objectAtIndex:0];
        _array=nil;
        
        if (_array==nil) {
            self.array=[NSMutableArray array];
            for (int i=1;i<arr.count ; i++) {
                NSArray *arr2=arr[i];
                [_array addObject:arr2];
            }
        }
    }
    else  if (_segmentVC.selectedSegmentIndex==1) {
        
        
        _array=nil;
        
        [self.view addSubview:_shooterView];
        NSDictionary *dic=_allArray[1];
        NSArray *arr=[dic objectForKey:@"ranks_data"];
        _titleArray=[arr objectAtIndex:0];
        if (_array==nil) {
            self.array=[NSMutableArray array];
            for (int i=1;i<arr.count ; i++) {
                NSArray *arr2=arr[i];
                [_array addObject:arr2];
            }
        }
        
    }else  if (_segmentVC.selectedSegmentIndex==2) {
        _array=nil;
        
        [self.view addSubview:_assistView];
        NSDictionary *dic=_allArray[2];
        NSArray *arr=[dic objectForKey:@"ranks_data"];
        _titleArray=arr[0];
        
        if (_array==nil) {
            self.array=[NSMutableArray array];
            for (int i=1;i<arr.count ; i++) {
                NSArray *arr2=arr[i];
                [_array addObject:arr2];
            }
        }
        
    }
    
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section
{

    return 30;
}
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section
{
    CGRect frame=[UIScreen mainScreen].bounds;
    UIView *headView=[[UIView alloc] initWithFrame:self.view.frame];
   // headView.backgroundColor=[UIColor colorWithRed:0.127 green:0.729 blue:0.725 alpha:1.000];
    headView.backgroundColor=[UIColor colorWithRed:0.276 green:0.419 blue:0.569 alpha:1.000];
    if (_segmentVC.selectedSegmentIndex==0) {
        UILabel *lable1=[[UILabel alloc] initWithFrame:CGRectMake(5, 0, CGRectGetWidth(headView.frame)/10, 30)];
        UILabel *lable2=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*2, 0, CGRectGetWidth(tableView.frame)/10*2, 30)];
        UILabel *lable3=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*4, 0, CGRectGetWidth(headView.frame)/10, 30)];
        UILabel *lable4=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*5, 0, CGRectGetWidth(headView.frame)/10, 30)];
        UILabel *lable5=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*6, 0, CGRectGetWidth(headView.frame)/10, 30)];
        UILabel *lable6=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*7, 0, CGRectGetWidth(headView.frame)/10, 30)];
        UILabel *lable7=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/10*8-5, 0, CGRectGetWidth(headView.frame)/10+5, 30)];
        UILabel *lable8=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(frame)/10*9, 0, CGRectGetWidth(headView.frame)/10, 30)];
        
        [headView addSubview:lable1];
        [headView addSubview:lable2];
        [headView addSubview:lable3];
        [headView addSubview:lable4];
        [headView addSubview:lable5];
        [headView addSubview:lable6];
        [headView addSubview:lable7];
        [headView addSubview:lable8];
        lable1.text=_titleArray[0];
        lable2.text=_titleArray[1];
        lable3.text=_titleArray[3];
        lable4.text=_titleArray[4];
        lable5.text=_titleArray[5];
        lable6.text=_titleArray[6];
        lable7.text=_titleArray[7];
        lable8.text=_titleArray[8];
        lable1.font=[UIFont systemFontOfSize:10.0];
        lable2.font=[UIFont systemFontOfSize:10.0];
        lable3.font=[UIFont systemFontOfSize:10.0];
        lable4.font=[UIFont systemFontOfSize:10.0];
        lable5.font=[UIFont systemFontOfSize:10.0];
        lable6.font=[UIFont systemFontOfSize:10.0];
        lable7.font=[UIFont systemFontOfSize:10.0];
        lable8.font=[UIFont systemFontOfSize:10.0];
    }
    if (_segmentVC.selectedSegmentIndex==1) {
        UILabel *numberLable=[[UILabel alloc] initWithFrame:CGRectMake(5, 0, CGRectGetWidth(headView.frame)/6, 30)];
        UILabel *nameLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6, 0, CGRectGetWidth(headView.frame)/6*2, 30)];
        nameLable.textAlignment=1;
        UILabel *teamLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*3, 0, CGRectGetWidth(headView.frame)/6*2, 30)];
        teamLable.textAlignment=1;
        UILabel *ballLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*5, 0, CGRectGetWidth(headView.frame)/6+10, 30)];
        [headView addSubview:numberLable];
        [headView addSubview:nameLable];
        [headView addSubview:teamLable];
        [headView addSubview:ballLable];
        numberLable.text=[NSString stringWithFormat:@"%@",_titleArray[0]];
        numberLable.textAlignment=1;
        nameLable.text=_titleArray[1];
        teamLable.text=_titleArray[3];
        ballLable.text=_titleArray[4];
       
        ballLable.font=[UIFont systemFontOfSize:12];
        numberLable.font=[UIFont systemFontOfSize:12];
        nameLable.font=[UIFont systemFontOfSize:12];
        teamLable.font=[UIFont systemFontOfSize:12];
     //   headView.backgroundColor=[UIColor colorWithRed:1.000 green:0.110 blue:0.240 alpha:1.000];
       
    }else
        if (_segmentVC.selectedSegmentIndex==2)
        {
           // headView.backgroundColor=[UIColor colorWithRed:1.000 green:0.103 blue:0.144 alpha:1.000];
            UILabel *numberLable=[[UILabel alloc] initWithFrame:CGRectMake(5, 0, CGRectGetWidth(headView.frame)/6, 30)];
            UILabel *nameLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6, 0, CGRectGetWidth(headView.frame)/6*2, 30)];
            nameLable.textAlignment=1;
            UILabel *teamLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*3, 0, CGRectGetWidth(headView.frame)/6*2, 30)];
            teamLable.textAlignment=1;
            UILabel *ballLable=[[UILabel alloc] initWithFrame:CGRectMake(CGRectGetWidth(headView.frame)/6*5, 0, CGRectGetWidth(headView.frame)/6+10, 30)];
            ballLable.textAlignment=1;
            [headView addSubview:numberLable];
            [headView addSubview:nameLable];
            [headView addSubview:teamLable];
            [headView addSubview:ballLable];
            numberLable.text=[NSString stringWithFormat:@"%@",_titleArray[0]];
            numberLable.textAlignment=1;
            nameLable.text=_titleArray[1];
            teamLable.text=_titleArray[3];
            ballLable.text=_titleArray[4];
            numberLable.font=[UIFont systemFontOfSize:13.0];
               nameLable.font=[UIFont systemFontOfSize:13.0];
               teamLable.font=[UIFont systemFontOfSize:13.0];
               ballLable.font=[UIFont systemFontOfSize:13.0];
            // ballLable.font=[UIFont systemFontOfSize:13.5];
//            numberLable.backgroundColor=[UIColor redColor];
//            nameLable.backgroundColor=[UIColor yellowColor];
//            teamLable.backgroundColor=[UIColor blueColor];
//            ballLable.backgroundColor=[UIColor redColor];
        
        }
    
    return headView;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    
    if (_segmentVC.selectedSegmentIndex==0) {
        
        
        SEGScoreDetaiController *scoreVC=[[SEGScoreDetaiController alloc] init];
        NSArray *arr= _array[indexPath.row];
        scoreVC.title=arr[1];
        scoreVC.string=arr[9];
//        CATransition *transition = [CATransition animation];
//        
//        transition.duration = 5.0f;
//        
//        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//        
//        transition.type = kCATransitionPush;
//        
//        transition.subtype = kCATransitionFromTop;
//        
//        transition.delegate = self;
//        
//        [scoreVC.view.layer addAnimation:transition forKey:nil];
        //        UINavigationController *NC=[[UINavigationController alloc] initWithRootViewController:scoreVC];
        // [self.view addSubview:scoreVC.view];
        // NSLog(@"%@",self.navigationController);//test
        // [self presentViewController:NC animated:YES completion:nil];
        [self.navigationController pushViewController:scoreVC animated:YES];
        
        
    }else
    {
        BingTouchController *ballVC=[[BingTouchController alloc] init];
        NSArray *arr= _array[indexPath.row];
        ballVC.title=arr[1];
        ballVC.str=arr[5];
        
//        CATransition *transition = [CATransition animation];
//        
//        transition.duration = 5.0f;
//        
//        transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionLinear];
//        
//        transition.type = kCATransitionPush;
//        
//        transition.subtype = kCATransitionFromRight;
//        
//        transition.delegate = self;
//        self.navigationController.navigationBar.translucent = NO;
//        [ballVC.view.layer addAnimation:transition forKey:nil];
        
        [self.navigationController pushViewController:ballVC animated:YES];
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{

    return 60;
}
@end
